package cn.virens.web.common;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;

import cn.virens.database.common.BaseService;
import cn.virens.util.page.Page;
import cn.virens.util.shiro.Permission;

/**
 * 表格类 通用控制器
 * 
 * @param <T>
 * @param <S>
 * @作者 : loioi
 * @创建时间 :2017年8月3日 下午3:48:14
 */
public abstract class AbstractTableController<T, S extends BaseService<T>> extends BaseUserController {
	protected S baseService;
	/** 添加接口是否可用 */
	protected boolean add = true;
	/** 编辑接口是否可用 */
	protected boolean edit = true;
	/** 删除接口是否可用 */
	protected boolean del = true;

	public AbstractTableController(String baseTemplateUrl, String identity, S baseService) {
		super(baseTemplateUrl, identity);
		this.baseService = baseService;
	}

	public AbstractTableController(String baseTemplateUrl, S baseService) {
		super(baseTemplateUrl);
		this.baseService = baseService;
	}

	/**
	 * 列表页面
	 * 
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequestMapping("index.jspx")
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		checkPermission(request, Permission.VIEW.parts(_identity));// 检查用户的权限是否可以访问

		this.savePageInfo(request, model);

		return returnUrl("index", model);
	}

	/**
	 * 获取数据接口
	 * 
	 * @param request
	 * @param model
	 * @return
	 * @throws Exception
	 */
	public abstract Model list(HttpServletRequest request, Model model) throws Exception;

	/**
	 * 添加数据接口
	 * 
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "save.json", params = "oper=add")
	public Model add(HttpServletRequest request, HttpServletResponse response, Model model, T record, BindingResult result) throws Exception {
		if (!add) return renderFail(model, "该接口未开放");
		checkPermission(request, Permission.MANAGE.parts(_identity));
		return addHandle(request, model, record);
	}

	/**
	 * 修改数据接口
	 * 
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "save.json", params = "oper=edit")
	public Model edit(HttpServletRequest request, HttpServletResponse response, Model model, T record, BindingResult result) throws Exception {
		if (!edit) return renderFail(model, "该接口未开放");
		checkPermission(request, Permission.MANAGE.parts(_identity));

		return editHandle(request, model, record);
	}

	/**
	 * 删除数据接口
	 * 
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping(value = "save.json", params = "oper=del")
	public Model del(HttpServletRequest request, HttpServletResponse response, Model model, @RequestParam(name = "id", required = true, defaultValue = "") String id) throws Exception {
		if (!del) return renderFail(model, "该接口未开放");
		checkPermission(request, Permission.MANAGE.parts(_identity));

		return delHandle(request, model, id);
	}

	/**
	 * 异常操作
	 */
	@ResponseBody
	@RequestMapping(value = "save.json")
	public Model other(Model model) {
		return renderFail(model, "没有有效的请求类型");
	}

	/**
	 * 查询数据库
	 * 
	 * @param request
	 * @param jqGrid
	 * @return
	 * @throws Exception
	 */
	protected PageInfo<?> getData(HttpServletRequest request, Page page) throws Exception {
		return baseService.selectPage(page);
	}

	/**
	 * 添加一条记录
	 * 
	 * @param request 请求
	 * @param model 返回
	 * @param record 数据
	 * @return
	 */
	protected Model addHandle(HttpServletRequest request, Model model, T record) throws Exception {
		if (baseService.insert(record) > 0) {
			return renderSuccess(model, "添加成功");
		} else {
			return renderFail(model, "添加失败");
		}
	}

	/**
	 * 更新一条记录
	 * 
	 * @param request 请求
	 * @param model 返回
	 * @param record 数据
	 * @return
	 */

	protected Model editHandle(HttpServletRequest request, Model model, T record) throws Exception {
		if (baseService.update(record) > 0) {
			return renderSuccess(model, "更新成功");
		} else {
			return renderFail(model, "更新失败");
		}
	}

	/**
	 * 删除
	 * 
	 * @param request 请求
	 * @param model 返回
	 * @param id
	 * @return
	 */
	protected Model delHandle(HttpServletRequest request, Model model, String id) throws Exception {
		if (baseService.delete(id) > 0) {
			return renderSuccess(model, "成功删除");
		} else {
			return renderFail(model, "删除失败");
		}
	}

	/**
	 * 保存 分页信息
	 * 
	 * @param request
	 * @param model
	 */
	protected abstract void savePageInfo(HttpServletRequest request, Model model);

	/**
	 * 设置save接口下的3个通用接口是否可用
	 * 
	 * @param add 添加接口
	 * @param edit 编辑接口
	 * @param del 删除接口
	 */
	protected final void setSaveUse(boolean add, boolean edit, boolean del) {
		this.add = add;
		this.edit = edit;
		this.del = del;
	}
}
